package cn.wolfcode.trip.admin;

import cn.wolfcode.trip.core.domain.StrategyCatalog;
import cn.wolfcode.trip.core.query.StrategyCatalogQueryObject;
import cn.wolfcode.trip.core.service.IStrategyCatalogService;
import cn.wolfcode.trip.core.service.IStrategyService;
import cn.wolfcode.trip.core.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("strategyCatalog")
public class StrategyCatalogController {

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyService strategyService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") StrategyCatalogQueryObject qo, Model model){
        // 根据序号排序
        qo.setOrderBy("sequence");
        model.addAttribute("pageInfo",strategyCatalogService.query(qo));
        model.addAttribute("strategies",strategyService.listAll());
        return "strategyCatalog/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(StrategyCatalog strategyCatalog) {
        JsonResult json = new JsonResult();
        try {
            strategyCatalogService.saveOrUpdate(strategyCatalog);
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    @RequestMapping("listByStrategyId")
    @ResponseBody
    public List<StrategyCatalog> listByStrategyId(Long strategyId){

        return strategyCatalogService.listByStrategyId(strategyId);
    }

}
